import edu.princeton.cs.algs4.StdOut;

import java.util.Iterator;

public class ResizingArrayStackStrings<Item> implements Iterable<Item>{
    private int N;
    private Item[] a = (Item[]) new Object[1];

    //构造函数


    //是否空
    public boolean isEmpty(){return N == 0;}

    //尺寸
    public int size(){return N;}

    public void push(Item item){
        if(N == a.length){
            StdOut.println(a.length);resize(2*a.length);}
        a[N++] = item;
    }
    public Item pop(){
        Item item = a[--N];
        a[N] = null;
        if(N > 0 && N == a.length/4 ){resize(a.length/2);}
        return item;
    }

    private void resize(int max){
        Item[] temp = (Item[]) new Object[max];
        for(int i =0 ; i <N;i++){
            temp[i] = a[i];
        }
        a = temp;
    }
    public Iterator<Item> iterator(){
        return new RverseArrayIterator();
    }
    private class RverseArrayIterator implements Iterator<Item> {
        private int i = N;
        public boolean hasNext() { return i>0; }
        public Item next() { return a[--i]; }
        public void remove(){}
    }

}